<?php
/**
 * Created by PhpStorm.
 * User: yangjiaming
 * Date: 2018/2/5
 * Time: 17:17
 */
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class Error
{
    public function __construct()
    {
        set_exception_handler(array($this, 'handleException'));
    }
    public function handleException(\Exception $e)
    {
        $msg = sprintf('Uncaught Exception %s: "%s" at %s line %s', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine());
        $log = new Logger('web');
        $log->pushHandler(new StreamHandler(APPLICATION_PATH.'/logs/'.date('Ymd').'/web.log', Logger::ERROR));
        $log->addError('', [$msg]);
        echo json_encode(['code' => '-1', 'msg' => $e->getMessage()]);
    }
}
$error = new Error();